package com.cslg.ac.demo.service.like;

import com.cslg.ac.demo.dao.pet.PetCommonDAO;
import com.cslg.ac.demo.entity.pet.PetCommon;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cslg.ac.demo.constant.LikeType;
import com.cslg.ac.demo.constant.ResponseCode;
import com.cslg.ac.demo.dao.like.LikeDAO;
import com.cslg.ac.demo.dao.user.UserDAO;
import com.cslg.ac.demo.dao.like.po.LikePO;

@Service
public class LikeService {

	@Autowired
	private LikeDAO likeDAO;
	@Autowired
	private UserDAO userDAO;
	@Autowired
	private PetCommonDAO petCommonDAO;
	private LikePO getLikePO(int id, int type, int targetId)
	{
		LikePO likePO = new LikePO();
		likePO.setId(id);
		likePO.setType(type);
		likePO.setTargetId(targetId);
		return likePO;
	}
	
	public int add(int id, int type, int targetId)
	{
		LikePO likePO = getLikePO(id, type, targetId);
		switch(type)
		{
			case LikeType.USER :
				if(userDAO.SelectById(targetId) == null) {
					return ResponseCode.USER_NOT_EXISTS;
				}
				if(likeDAO.exist(likePO) != null) {
					return ResponseCode.REQUEST_SUCCESSED;
				}
				break;
			case LikeType.PET :
				if(petCommonDAO.selectById(targetId) == null){
					return ResponseCode.USER_NOT_EXISTS;
				}
				if(likeDAO.exist(likePO) != null) {
					return ResponseCode.REQUEST_SUCCESSED;
				}
				break;
			default : break;
		}
		likeDAO.insert(likePO);
		return ResponseCode.REQUEST_SUCCESSED;
	}
	
	public int delete(int id, int type, int targetId)
	{
		likeDAO.delete(getLikePO(id, type, targetId));
		return ResponseCode.REQUEST_SUCCESSED;
	}
	
	public int[] getAll(int id, int type)
	{
		int[] ids = likeDAO.select(id, type);
		return ids;
	}
	
	public int[] follow(int id, int type)
	{
		int[] ids = likeDAO.selectFollow(id, type);
		return ids;
	}
	
}
